package com.example.administrator.taxicar.ui.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.administrator.taxicar.R;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class PingjiaActivity extends Activity implements View.OnClickListener {

    private EditText pingjia_edit;
    private TextView manyi_text;
    private Button pingjia_button;
    private RatingBar xing_bar;
    private float currentRating;
    private ImageView call_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
        setContentView(R.layout.activity_pingjia_layout);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.pingjia_titlebar_layout);//自定义布局赋

        findViewById(R.id.title_left_img).setOnClickListener(this);
        pingjia_edit = (EditText) findViewById(R.id.pingjia_edit);
        manyi_text = (TextView) findViewById(R.id.manyi_text);
        pingjia_button = (Button) findViewById(R.id.pingjia_button);
        pingjia_button.setOnClickListener(this);

        xing_bar = (RatingBar) findViewById(R.id.xing_bar);
        //得到RatingBar的最大等级
        int max = xing_bar.getMax();
        //得到RatingBar现在的等级
        currentRating = xing_bar.getRating();
        xing_bar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                Log.e("tag", "现在的等级为 rating=" + rating + ",是否是用户触发 fromUser=" + fromUser);
                manyi_text.setVisibility(View.VISIBLE);
                if (rating <= 1) {
                    manyi_text.setText("非常不满意，各方面都很差");
                } else if (rating > 1 && rating <= 2) {
                    manyi_text.setText("不满意，比较差");
                } else if (rating > 2 && rating <= 3) {
                    manyi_text.setText("一般，需要改善");
                } else if (rating > 3 && rating <= 4) {
                    manyi_text.setText("比较满意，但仍可改善");
                } else if (rating > 4 && rating <= 5) {
                    manyi_text.setText("非常满意，无可挑剔");
                }
                pingjia_edit.setVisibility(View.VISIBLE);
                pingjia_button.setVisibility(View.VISIBLE);
                currentRating = rating;
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_img:
                finish();
                break;
            case R.id.pingjia_button:
                //提交
                String pingjia = pingjia_edit.getText().toString();

                final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                        .setTitleText("正在加载数据");
                pDialog.show();
                pDialog.setCancelable(false);
                new CountDownTimer(800 * 2, 800) {
                    public void onTick(long millisUntilFinished) {
                        pDialog.getProgressHelper().setBarColor(Color.parseColor("#0392ff"));
                    }
                    public void onFinish() {
                        pDialog.dismiss();
                        manyi_text.setText("谢谢您的评价");
                        xing_bar.setRating(currentRating);
                        pingjia_edit.setVisibility(View.GONE);
                        pingjia_button.setVisibility(View.GONE);
                        xing_bar.setIsIndicator(true);
                    }
                }.start();
                break;
            case R.id.call_phone:

                break;
        }
    }
}
